package com.seedwill.common.query.schema;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import com.seedwill.common.util.SchemIdGen;

/**
 * 
 *  查询条件组
 * 
 * @author kdyi
 *
 */
public class QueryCriteriaGroup extends QueryCriteriaGroupSchema implements Comparator {
	
	private String groupInstanceId;
	
	private int groupInstanceIndex;
	
	/**
	 * 子查询条件组
	 */
	protected List<QueryCriteriaGroup> childrenGroup;
	
	/**
	 * 当前条件
	 */
	private List<QueryCriteria> criteriaes;
	
	/**
	 * 默认构造
	 */
	public QueryCriteriaGroup() {
		init(false);
		groupInstanceIndex = groupIndex;
		groupInstanceId = SchemIdGen.genSurfixId(groupSchemaId);
	}
	
	 /**
	 * @param mainId
	 */
	public QueryCriteriaGroup(String mainId) {
			init(false);
			groupInstanceIndex = groupIndex;
			groupInstanceId = mainId;
	}

	/**
	 * 初始化
	 */
	public void init(boolean initChildren){
		super.init(initChildren);
		if(null == criteriaes){
			criteriaes = new ArrayList<QueryCriteria>();
		}
		if(initChildren){
			if(null == childrenGroup){
				childrenGroup = new ArrayList<QueryCriteriaGroup>();
			}
		}

	}
	
	public void syncSchema(){
		
	}
	
	
	/**
	 * 比较
	 * @param o1
	 * @param o2
	 * @return
	 */
	public int compare(Object o1, Object o2) {
		if(null != o1 && null != o2){
			return ((QueryCriteriaGroup)o1).getGroupInstanceIndex() - ((QueryCriteriaGroup)o2).getGroupInstanceIndex();
		}else{
			return 0;
		}
	}
	
	/**
	 * 条件是否存在
	 * @param criteriaId
	 * @return
	 */
	public boolean isCriteriaExisted(String criteriaId){
		init(false);
		boolean flag = false;
		for(QueryCriteria cr : criteriaes){
			if(null != cr && null != cr.getSchemaId() && cr.getSchemaId().equals(criteriaId)){
				flag = true;
				break;
			}
		}
		return flag;
	}
	/**
	 * 增加
	 */
	public void addCriteria(QueryCriteria criteria)  throws AddEelmentException{
		init(false);
		//
		if(!isCriteriaExisted(criteria.getSchemaId())){
			criteriaes.add(criteria);
		}else{
			 throw new AddEelmentException("Criteria "+criteria.getSchemaId()+" already exists"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	/**
	 * 修改条件
	 */
	public void resetCriteria(QueryCriteria criteria)  throws ResetEelmentException{
		init(false);
		//
		int idx = criteriaIndex(criteria.getSchemaId());
		if(idx>=0){
			criteriaes.set(idx, criteria);
		}else{
			 throw new ResetEelmentException("Criteria "+criteria.getSchemaId()+" already exists"); //$NON-NLS-1$
		}
	}
	
	/**
	 * 指定条件的索引
	 * @param criteriaId
	 * @return
	 */
	public int criteriaIndex(String criteriaId) {
		int idx = -1 ;
		for(idx=0; idx< criteriaSchemaes.size(); idx++){
			QueryCriteria cr = criteriaes.get(idx);
			if(null != cr && null != cr.getSchemaId() && cr.getSchemaId().equals(criteriaId)){
				break;
			}
		}
		return idx;
	}


	/**
	 * 删除
	 * @param criteriaId
	 * @throws RemoveEelementException
	 */
	public void removeCriteria(String criteriaId) throws RemoveEelementException {
		init(false);
		if(isCriteriaExisted(criteriaId)){
			for(QueryCriteria cr : criteriaes){
				if(null != cr && null != cr.getSchemaId() && cr.getSchemaId().equals(criteriaId)){
					criteriaSchemaes.remove(cr);
					break;
				}
			}
		}else{
			throw new RemoveEelementException("no such element to remove:"+criteriaId); //$NON-NLS-1$
		}
	}
	/**
	 * 清除
	 * @throws CleanEelementException
	 */
	public void cleanCriteria() throws CleanEelementException {
		init(false);
		criteriaes.clear();
	}
	
	public void cleanChildrenGroupCriteria() throws CleanEelementException {
		init(false);
		childrenGroup.clear();
	}
	
	public void cleanAll(){
		cleanCriteria();
		cleanChildrenGroupCriteria();
	}
	
	public List<QueryCriteriaGroup> getChildrenGroup() {
		return childrenGroup;
	}
	public void setChildrenGroup(List<QueryCriteriaGroup> childrenGroup) {
		this.childrenGroup = childrenGroup;
	}
	public String getGroupInstanceId() {
		return groupInstanceId;
	}
	public void setGroupInstanceId(String groupInstanceId) {
		this.groupInstanceId = groupInstanceId;
	}
	public List<QueryCriteria> getCriteriaes() {
		return criteriaes;
	}
	public void setCriteriaes(List<QueryCriteria> criteriaes) {
		this.criteriaes = criteriaes;
	}
	public int getGroupInstanceIndex() {
		return groupInstanceIndex;
	}
	public void setGroupInstanceIndex(int groupInstanceIndex) {
		this.groupInstanceIndex = groupInstanceIndex;
	}
	
	
}
